//
//  AddJobViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/20.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "AddJobViewController.h"
#import "JobCategoryInfo.h"
#import "CZHAddressPickerView.h"
#import "SureMultipleSelectedWindow.h"
#import "SureConditionModel.h"
#import "JobDetailInfo.h"
#import "UITextView+Placeholder.h"

@interface AddJobViewController ()<UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource>

@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIView *pickerBgView;
@property (strong, nonatomic) UIView *pickerToolbgView;
@property (strong, nonatomic) UIButton *returnBtn;
@property (strong, nonatomic) UIView *headBgView;
@property (nonatomic, assign) BOOL isReturn;

@property (nonatomic, strong) NSString *jobCategoryStr;
@property (nonatomic, strong) NSString *district;

@property (strong, nonatomic) NSMutableArray *jobCategoryInfoArr;
@property (strong, nonatomic) NSArray *educationInfoArr;
@property (strong, nonatomic) NSArray *salaryInfoArr;
@property (strong, nonatomic) NSArray *workLifeInfoArr;
@property (strong, nonatomic) NSArray *workTypeInfoArr;

@property (strong, nonatomic) UIPickerView *educationPicker;
@property (strong, nonatomic) UIPickerView *categoryPicker;
@property (strong, nonatomic) UIPickerView *salaryPicker;
@property (strong, nonatomic) UIPickerView *workLifePicker;
@property (strong, nonatomic) UIPickerView *workTypePicker;			//工作性质
@property (strong, nonatomic) UIPickerView *welfarePicker;			//福利亮点

@property (strong, nonatomic) JobDetailInfo *model;

@end

@implementation AddJobViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self getCategory];
	if (self.type == 1) {
		[self getJobInfo];
	}
	[self createUI];
}

- (void)createUI {
	self.view.backgroundColor = UISeparatedColorRGB;
	_headBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
	[self.view addSubview:_headBgView];
	_headBgView.backgroundColor = UIMainColorRGB;
	
	UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(SW(2)-50, 25, 100, 30)];
	if (self.type == 0) {
		titleL.text = @"发布职位";
	}
	else{
		titleL.text = @"编辑岗位信息";
	}
	
	titleL.textColor = kWhiteColor;
	titleL.textAlignment = NSTextAlignmentCenter;
	[_headBgView addSubview:titleL];
	
	// 返回btn
	_returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	//[_headBgView addSubview:_returnBtn];
	[_returnBtn setImage:IMAGE(@"return") forState:UIControlStateNormal];
	[_returnBtn addTarget:self action:@selector(navLeftAction:) forControlEvents:UIControlEventTouchUpInside];
	_returnBtn.frame = CGRectMake(0, 20, 44, 44);
	[self.view addSubview:_returnBtn];
	
	_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _headBgView.height, SCREEN_WIDTH, SCREEN_HEIGHT-_headBgView.height)];
	[self.view addSubview:_scrollView];
	NSArray *arr = [NSArray arrayWithObjects:@"   职位类型*", @"   工作经验*", @"   学历要求*", @"   薪资范围*", @"   地区*", @"   详细地址*", @"   工作性质*", @"   福利亮点*", @"   发布人*", @"   发布人职位*", @"   岗位职责*", @"   任职要求*", nil];
	
	CGSize size = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(15)];
	
	for (int i = 0; i < arr.count; i++) {
		UILabel *label = [[UILabel alloc] init];
		[_scrollView addSubview:label];
		label = [self createLabel:label text:arr[i]];
		label.tag = 1+i;
	}
	
	UILabel *jobCateL = [self.view viewWithTag:1];
	jobCateL.frame = CGRectMake(0, 0, SCREEN_WIDTH, size.height+30);
	[self createTextFieldWith:jobCateL tag:21 pickerNum:1];
	
	UILabel *workLifeL = [self.view viewWithTag:2];
	workLifeL.frame = CGRectMake(0, jobCateL.y+jobCateL.height+1, SCREEN_WIDTH, size.height+30);
	[self createTextFieldWith:workLifeL tag:22 pickerNum:2];
	
	UILabel *educationL = [self.view viewWithTag:3];
	educationL.frame = CGRectMake(0, workLifeL.y+workLifeL.height+1, SCREEN_WIDTH, size.height+30);
	[self createTextFieldWith:educationL tag:23 pickerNum:3];
	
	UILabel *salaryL = [self.view viewWithTag:4];
	salaryL.frame = CGRectMake(0, educationL.y+educationL.height+1, SCREEN_WIDTH, size.height+30);
	[self createTextFieldWith:salaryL tag:24 pickerNum:4];
	
	UILabel *districtL = [self.view viewWithTag:5];
	districtL.frame = CGRectMake(0, salaryL.y+salaryL.height+1, SCREEN_WIDTH, size.height+30);
	UITextField *districtTF = [self createTextFieldWith:districtL tag:25 pickerNum:0];
	UIView *viewDistrict = [[UIView alloc] initWithFrame:CGRectMake(districtTF.x, districtTF.y, districtTF.width, districtTF.height)];
	[_scrollView addSubview:viewDistrict];
	[viewDistrict addActionWithblock:^{
		NSLog(@"选择地区");
		[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
		[CZHAddressPickerView areaPickerViewWithAreaBlock:^(NSString *province, NSString *city, NSString *area) {
			districtTF.text = [NSString stringWithFormat:@"%@%@%@", province, city, area];
			self.district = [NSString stringWithFormat:@"%@%@", city, area];
		}];
	}];
	
	UILabel *addressL = [self.view viewWithTag:6];
	addressL.frame = CGRectMake(0, districtL.y+districtL.height+1, SCREEN_WIDTH, size.height+30);
	[self createTextFieldWith:addressL tag:26 pickerNum:0];
	
	UILabel *workTypeL = [self.view viewWithTag:7];
	workTypeL.frame = CGRectMake(0, addressL.y+addressL.height+1, SCREEN_WIDTH, size.height+30);
	[self createTextFieldWith:workTypeL tag:27 pickerNum:7];
	
	UILabel *welfareL = [self.view viewWithTag:8];
	welfareL.frame = CGRectMake(0, workTypeL.y+workTypeL.height+1, SCREEN_WIDTH, size.height+30);
	UITextView *welfareTV = [[UITextView alloc] init];
	[_scrollView addSubview:welfareTV];
	welfareTV.textColor = [UIColor lightGrayColor];
	welfareTV.font = kUHSystemFontWithSize(14);
	welfareTV.delegate = self;
	welfareTV.placeholder = @"点击选择";
	[welfareTV scrollRangeToVisible:NSMakeRange(0,1)];
	CGSize signSize = [welfareTV sizeThatFits:CGSizeMake(SCREEN_WIDTH-20-SW(20), MAXFLOAT)];
	welfareTV.frame = CGRectMake(0, welfareL.y+welfareL.height+1, SCREEN_WIDTH, 15+signSize.height*1.5);
	welfareTV.textContainerInset = UIEdgeInsetsMake(15, SW(20), 15, 20);//设置页边
	welfareTV.tag = 28;
	UIView *viewWelfare = [[UIView alloc] initWithFrame:CGRectMake(welfareTV.x, welfareTV.y, welfareTV.width, welfareTV.height)];
	[_scrollView addSubview:viewWelfare];
	[viewWelfare addActionWithblock:^{
		NSLog(@"选择福利");
		[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
		[SureMultipleSelectedWindow showWindowWithTitle:@"选择福利" selectedConditions:@[@"五险一金", @"带薪休假", @"公司旅游", @"绩效奖金", @"周末双休"] defaultSelectedConditions:@[@"五险一金"] selectedBlock:^(NSArray *selectedArr) {
			NSString *welfareStr = @"";
			for (SureConditionModel *model in selectedArr)
			{
				NSString *str = model.title;
				welfareStr = [welfareStr stringByAppendingString:[NSString stringWithFormat:@"%@  ",str]];
				NSLog(@"福利%@", str);
			}
			welfareTV.text = welfareStr;
		}];
	}];
	
	UILabel *publisherL = [self.view viewWithTag:9];
	publisherL.frame = CGRectMake(0, welfareTV.y+welfareTV.height+1, SCREEN_WIDTH, size.height+30);
	[self createTextFieldWith:publisherL tag:29 pickerNum:0];
	
	UILabel *publisherJobL = [self.view viewWithTag:10];
	publisherJobL.frame = CGRectMake(0, publisherL.y+publisherL.height+1, SCREEN_WIDTH, size.height+30);
	[self createTextFieldWith:publisherJobL tag:30 pickerNum:0];
	
	UILabel *dutyL = [self.view viewWithTag:11];
	dutyL.frame = CGRectMake(0, publisherJobL.y+publisherJobL.height+1, SCREEN_WIDTH, size.height+30);
	UITextView *dutyTV = [self createTextViewWith:dutyL tag:31 heightFactor:2.0];
	dutyTV.placeholder = @"请填写岗位职责";
	
	UILabel *demandL = [self.view viewWithTag:12];
	demandL.frame = CGRectMake(0, dutyTV.y+dutyTV.height+1, SCREEN_WIDTH, size.height+30);
	UITextView *demandTV = [self createTextViewWith:demandL tag:32 heightFactor:2.0];
	demandTV.placeholder = @"请填写任职要求";
	
	
	//初始化PickerView相关组件
	_pickerBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
	[self.view addSubview:_pickerBgView];
	_pickerBgView.backgroundColor = RGBA(0, 0, 0, 0.2);
	_pickerBgView.hidden = YES;
	
	_categoryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 2*SCREEN_HEIGHT, SCREEN_WIDTH, SH(3))];
	[self.view addSubview:_categoryPicker];
	_categoryPicker.delegate = self;
	_categoryPicker.dataSource = self;
	_categoryPicker.backgroundColor = UISeparatedColorRGB;
	_categoryPicker.showsSelectionIndicator = YES;
	_categoryPicker.hidden = YES;
	
	_salaryInfoArr = @[@"2000以下", @"2k-4k", @"4k-6k", @"6k-8k", @"8k-10k", @"10k-15k", @"15k-20k", @"20k-30k", @"30k-40k", @"40k-50k", @"5万以上"];
	_salaryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 2*SCREEN_HEIGHT, SCREEN_WIDTH, SH(3))];
	[self.view addSubview:_salaryPicker];
	_salaryPicker.delegate = self;
	_salaryPicker.dataSource = self;
	_salaryPicker.backgroundColor = UISeparatedColorRGB;
	_salaryPicker.showsSelectionIndicator = YES;
	_salaryPicker.hidden = YES;
	
	
	_educationInfoArr = @[@"初中及以下", @"中专/中技", @"高中", @"大专", @"本科", @"硕士", @"博士"];
	_educationPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 2*SCREEN_HEIGHT, SCREEN_WIDTH, SH(3))];
	[self.view addSubview:_educationPicker];
	_educationPicker.delegate = self;
	_educationPicker.dataSource = self;
	_educationPicker.backgroundColor = UISeparatedColorRGB;
	_educationPicker.showsSelectionIndicator = YES;
	_educationPicker.hidden = YES;

	
	_workLifeInfoArr = @[@"应届生", @"1年以内", @"1-3年", @"3-5年", @"5-10年", @"10年以上"];
	_workLifePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 2*SCREEN_HEIGHT, SCREEN_WIDTH, SH(3))];
	[self.view addSubview:_workLifePicker];
	_workLifePicker.delegate = self;
	_workLifePicker.dataSource = self;
	_workLifePicker.backgroundColor = UISeparatedColorRGB;
	_workLifePicker.showsSelectionIndicator = YES;
	_workLifePicker.hidden = YES;
	
	_workTypeInfoArr = @[@"全职", @"兼职"];
	_workTypePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 2*SCREEN_HEIGHT, SCREEN_WIDTH, SH(3))];
	[self.view addSubview:_workTypePicker];
	_workTypePicker.delegate = self;
	_workTypePicker.dataSource = self;
	_workTypePicker.backgroundColor = UISeparatedColorRGB;
	_workTypePicker.showsSelectionIndicator = YES;
	_workTypePicker.hidden = YES;
	
	_pickerToolbgView = [[UIView alloc] initWithFrame:CGRectMake(0, 2*SCREEN_HEIGHT, SCREEN_WIDTH, 40)];
	[self.view addSubview:_pickerToolbgView];
	_pickerToolbgView.backgroundColor = UISeparatedColorRGB;
	
	NSArray *arr2 = @[@"取消", @"确定"];
	for (int i = 0; i < arr2.count; i++) {
		UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
		[_pickerToolbgView addSubview:btn];
		[btn setTitle:arr2[i] forState:UIControlStateNormal];
		[btn setTitleColor:kBlackColor forState:UIControlStateNormal];
		btn.titleLabel.font = kUHSystemFontWithSize(12);
		if (i==0) {
			btn.frame = CGRectMake(0, 0, 50, 40);
			[btn addTarget:self action:@selector(pickerRemove) forControlEvents:UIControlEventTouchUpInside];
		}
		else {
			btn.frame = CGRectMake(SCREEN_WIDTH-50, 0, 50, 40);
			[btn addTarget:self action:@selector(priceBtnText) forControlEvents:UIControlEventTouchUpInside];
		}
	}
	UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 39, SCREEN_WIDTH, 1)];
	[_pickerToolbgView addSubview:line];
	line.backgroundColor = kLightGrayColor;
	
	//保存按钮
	UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, demandTV.y+demandTV.height+20,  SCREEN_WIDTH-30*2, 40)];
	saveBtn.layer.cornerRadius = saveBtn.height/4;
	[saveBtn setTitle:@"保存" forState:UIControlStateNormal];
	[saveBtn setBackgroundColor:UIMainColorRGB];
	[saveBtn addTarget:self action:@selector(completeAction:) forControlEvents:UIControlEventTouchUpInside];
	[_scrollView addSubview:saveBtn];
	
	_scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, saveBtn.y+saveBtn.height+20);
}

#pragma mark ==========UIPickerView==========
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
	//    return 2; // 返回1表明该控件只包含1列
	
	if ([pickerView isEqual:_salaryPicker] || [pickerView isEqual:_educationPicker] || [pickerView isEqual:_workLifePicker] || [pickerView isEqual:_workTypePicker])
	{
		return 1;
	}
	else {
		return 2;
	}
}
//UIPickerViewDataSource中定义的方法，该方法的返回值决定该控件指定列包含多少个列表项
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	// 由于该控件只包含一列，因此无须理会列序号参数component
	// 该方法返回books.count，表明books包含多少个元素，该控件就包含多少行
	//    return self.pickerArr.count;
	if([pickerView isEqual:_categoryPicker]){
		if (component == 0) {
			return self.jobCategoryInfoArr.count;
		}
		else {
			
			if (self.jobCategoryInfoArr.count) {
				JobCategoryInfo *model = self.jobCategoryInfoArr[[pickerView selectedRowInComponent:0]];
				NSArray *array = model.child;
				if (array != nil && ![array isKindOfClass:[NSNull class]] && array.count != 0) {
					return array.count;
				}
				else{
					return 0;
				}
			}
			else{
				return 0;
			}
		}
	}
	else if([pickerView isEqual:_salaryPicker]){
		return self.salaryInfoArr.count;
	}
	else if([pickerView isEqual:_educationPicker]){
		return self.educationInfoArr.count;
	}
	else if([pickerView isEqual:_workLifePicker]){
		return self.workLifeInfoArr.count;
	}
	else if([pickerView isEqual:_workTypePicker]){
		return self.workTypeInfoArr.count;
	}
	else {
		return 0;
	}
}
// UIPickerViewDelegate中定义的方法，该方法返回的NSString将作为UIPickerView
// 中指定列和列表项的标题文本
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	// 由于该控件只包含一列，因此无须理会列序号参数component
	// 该方法根据row参数返回books中的元素，row参数代表列表项的编号，
	// 因此该方法表示第几个列表项，就使用books中的第几个元素
	//    return [self.pickerArr objectAtIndex:row];

	if([pickerView isEqual:_categoryPicker]) {
		if (component == 0) {
			JobCategoryInfo *model = self.jobCategoryInfoArr[row];
			return model.title;
		} else {
			JobCategoryInfo *model = self.jobCategoryInfoArr[[pickerView selectedRowInComponent:0]];
			NSArray *array = model.child;
			if (array != nil && ![array isKindOfClass:[NSNull class]] && array.count != 0 && array.count >= row+1) {
				return [array[row] objectForKey:@"title"];
			}
			else {
				return @"";
			}
		}
	}
	else if([pickerView isEqual:_salaryPicker]) {
		return self.salaryInfoArr[row];
	}
	else if([pickerView isEqual:_educationPicker]) {
		return self.educationInfoArr[row];
	}
	else if([pickerView isEqual:_workLifePicker]) {
		return self.workLifeInfoArr[row];
	}
	else if([pickerView isEqual:_workTypePicker]) {
		return self.workTypeInfoArr[row];
	}
	else{
		return @"";
	}
}
// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	//    [_priceSortedBtn setTitle:self.pickerArr[row] forState:UIControlStateNormal];
	//    self.pickerStr = self.pickerArr[row];
	if ([pickerView isEqual:_categoryPicker]) {
		
		[pickerView reloadComponent:1];
		[pickerView reloadComponent:0];
	}
}

- (void)priceBtnText {
	
	if(_categoryPicker.isHidden == NO) {
		JobCategoryInfo *parent = self.jobCategoryInfoArr[[_categoryPicker selectedRowInComponent:0]];
		NSArray *children = parent.child;
		if (children != nil && ![children isKindOfClass:[NSNull class]] && children.count != 0)
		{
			self.jobCategoryStr = children[[_categoryPicker selectedRowInComponent:1]][@"title"];
			self.selectedCategoryID = children[[_categoryPicker selectedRowInComponent:1]][@"id"];
			UITextField *textField = (UITextField *)[self.view viewWithTag:21];
			textField.text = self.jobCategoryStr;
			NSLog(@"the selected jobID is %@", self.selectedCategoryID);
		}
		//如果没有子分类就用父级的信息
		else {
			self.jobCategoryStr = parent.title;
			self.selectedCategoryID = parent.cateId;
			UITextField *textField = (UITextField *)[self.view viewWithTag:21];
			textField.text = self.jobCategoryStr;
			NSLog(@"the selected jobID is %@", self.selectedCategoryID);
		}
		
		[self pickerRemove];
	}
	else if(_salaryPicker.isHidden == NO)
	{
		UITextField *textField = (UITextField *)[self.view viewWithTag:24];
		textField.text = self.salaryInfoArr[[_salaryPicker selectedRowInComponent:0]];
		[self pickerRemove];
	}
	else if(_educationPicker.isHidden == NO)
	{
		UITextField *textField = [self.view viewWithTag:23];
		textField.text = self.educationInfoArr[[_educationPicker selectedRowInComponent:0]];
		[self pickerRemove];
	}
	else if(_workLifePicker.isHidden == NO)
	{
		UITextField *textField = [self.view viewWithTag:22];
		textField.text = self.workLifeInfoArr[[_workLifePicker selectedRowInComponent:0]];
		[self pickerRemove];
	}
	else if(_workTypePicker.isHidden == NO)
	{
		UITextField *textField = [self.view viewWithTag:27];
		textField.text = self.workTypeInfoArr[[_workTypePicker selectedRowInComponent:0]];
		[self pickerRemove];
	}
}

- (void)pickerShow:(NSInteger)picker {
	
	[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
	
	if(picker == 1){
		[self getCategory];
		_categoryPicker.hidden = NO;
		_pickerBgView.hidden = NO;
		[UIView animateWithDuration:0.33 animations:^{
			_pickerToolbgView.y = SH(3)*2-40;
			_categoryPicker.y = SH(3)*2;
		} completion:^(BOOL finished) {
		}];
	}
	else if(picker == 4){
		_salaryPicker.hidden = NO;
		_pickerBgView.hidden = NO;
		[UIView animateWithDuration:0.33 animations:^{
			_pickerToolbgView.y = SH(3)*2-40;
			_salaryPicker.y = SH(3)*2;
		} completion:^(BOOL finished) {
		}];
	}
	else if(picker == 3){
		_educationPicker.hidden = NO;
		_pickerBgView.hidden = NO;
		[UIView animateWithDuration:0.33 animations:^{
			_pickerToolbgView.y = SH(3)*2-40;
			_educationPicker.y = SH(3)*2;
		} completion:^(BOOL finished) {
		}];
	}
	else if(picker == 2){
		_workLifePicker.hidden = NO;
		_pickerBgView.hidden = NO;
		[UIView animateWithDuration:0.33 animations:^{
			_pickerToolbgView.y = SH(3)*2-40;
			_workLifePicker.y = SH(3)*2;
		} completion:^(BOOL finished) {
		}];
	}
	else if(picker == 7){
		_workTypePicker.hidden = NO;
		_pickerBgView.hidden = NO;
		[UIView animateWithDuration:0.33 animations:^{
			_pickerToolbgView.y = SH(3)*2-40;
			_workTypePicker.y = SH(3)*2;
		} completion:^(BOOL finished) {
		}];
	}
}
- (void)pickerRemove {
	
	[UIView animateWithDuration:0.33 animations:^{
		_pickerToolbgView.y = 2*SCREEN_HEIGHT;
		_categoryPicker.y = 2*SCREEN_HEIGHT;
		_salaryPicker.y = 2*SCREEN_HEIGHT;
		_educationPicker.y = 2*SCREEN_HEIGHT;
		_workLifePicker.y = 2*SCREEN_HEIGHT;
		_welfarePicker.y = 2*SCREEN_HEIGHT;
		_workTypePicker.y = 2*SCREEN_HEIGHT;
	} completion:^(BOOL finished) {
		_pickerBgView.hidden = YES;
		_categoryPicker.hidden = YES;
		_salaryPicker.hidden = YES;
		_educationPicker.hidden = YES;
		_workLifePicker.hidden = YES;
		_welfarePicker.hidden = YES;
		_workTypePicker.hidden = YES;
	}];
}

- (UILabel *)createLabel:(UILabel *)label text:(NSString *)text {
	
	label.textColor = [UIColor grayColor];
	label.text = text;
	label.font = kUHSystemFontWithSize(15);
	label.backgroundColor = kWhiteColor;
	return label;
}

- (UITextView *)createTextViewWith:(UILabel *)label tag:(NSInteger)tag heightFactor:(CGFloat)heightFactor
{
	UITextView *textView = [[UITextView alloc] init];
	[_scrollView addSubview:textView];
	textView.textColor = [UIColor lightGrayColor];
	textView.font = kUHSystemFontWithSize(14);
	textView.delegate = self;
	[textView scrollRangeToVisible:NSMakeRange(0,1)];
	CGSize signSize = [textView sizeThatFits:CGSizeMake(SCREEN_WIDTH-20-SW(20), MAXFLOAT)];
	textView.frame = CGRectMake(0, label.y+label.height+1, SCREEN_WIDTH, 15+signSize.height*heightFactor);
	textView.textContainerInset = UIEdgeInsetsMake(15, SW(20), 15, 20);//设置页边
	textView.tag = tag;
	return textView;
}

- (UITextField *)createTextFieldWith:(UILabel *)label tag:(NSInteger)tag pickerNum:(NSInteger)picker
{
	CGSize size = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(15)];
	UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width, label.y, SCREEN_WIDTH-(50+size.width), size.height+30)];
	[_scrollView addSubview:textField];
	textField.delegate = self;
	textField.textColor = kGrayColor;
	textField.clearButtonMode = UITextFieldViewModeWhileEditing;
	textField.font = kUHSystemFontWithSize(15);
	textField.textAlignment = NSTextAlignmentRight;
	textField.rightViewMode = UITextFieldViewModeAlways;
	textField.tag = tag;
	UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.height/2, size.height)];
	textField.rightView = imgV;
	imgV.image = IMAGE(@"arrow_right_grey");
	
	if (picker!=0) {
		UIView *view = [[UIView alloc] initWithFrame:CGRectMake(textField.x, textField.y, textField.width, textField.height)];
		[_scrollView addSubview:view];
		[view addActionWithblock:^{
			NSLog(@"选择器%ld",picker);
			[self pickerShow:picker];
		}];
	}
	return textField;
}

- (void)getCategory {
	
	[[NetworkTool sharedTool] requestWithName:@"获取职位分类列表" URLString:APIRecruitCategoryList parameters:nil method:@"GET" callBack:^(id responseObject) {
		
		NSArray *arr = responseObject[@"data"];
		[_jobCategoryInfoArr removeAllObjects];
		for (NSDictionary *dict in arr)
		{
			JobCategoryInfo *model = [[JobCategoryInfo alloc] init];
			[model setValuesForKeysWithDictionary:dict];
			[_jobCategoryInfoArr addObject:model];
		}
		
		[_categoryPicker reloadComponent:0];
		[_categoryPicker reloadComponent:1];
		
	}];
}

- (void)getJobInfo {
	
	NSDictionary *param = @{@"position_id" : self.positionId};
	[[NetworkTool sharedTool] requestWithName:@"获取职位详情" URLString:APIJobDetail parameters:param method:@"POST" callBack:^(id responseObject) {
		NSDictionary *dict = responseObject[@"data"];
		JobDetailInfo *model = [[JobDetailInfo alloc] init];
		[model setValuesForKeysWithDictionary:dict];
		self.model = model;
	}];
}

- (void)navLeftAction:(id)sender {
	if (self.isReturn) {
		[self Return];
	}
	else
		[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)Return {
	NSLog(@"++++++%d", self.isReturn);
	
	if (self.isReturn) {
		
		UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否放弃修改？" message:nil preferredStyle:UIAlertControllerStyleAlert];
		[alert addAction:[UIAlertAction actionWithTitle:@"返回上一层" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
			self.isReturn = 0;
			[self dismissViewControllerAnimated:YES completion:nil];
		}]];
		[alert addAction:[UIAlertAction actionWithTitle:@"继续编辑" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		}]];
		[self presentViewController:alert animated:YES completion:nil];
	}
}

- (void)completeAction:(UIButton *)sender {
	NSLog(@"完成信息编辑");
	UITextField *jobCategoryTF = (UITextField *)[self.view viewWithTag:21];
	UITextField *workLifeTF = (UITextField *)[self.view viewWithTag:22];
	UITextField *educationTF = (UITextField *)[self.view viewWithTag:23];
	UITextField *salaryTF = (UITextField *)[self.view viewWithTag:24];
	UITextField *districtTF = (UITextField *)[self.view viewWithTag:25];
	UITextField *addressTF = (UITextField *)[self.view viewWithTag:26];
	UITextField *workTypeTF = (UITextField *)[self.view viewWithTag:27];
	UITextView *welfareTV = (UITextView *)[self.view viewWithTag:28];
	UITextField *publisherTF = (UITextField *)[self.view viewWithTag:29];
	UITextField *publisherJobTF = (UITextField *)[self.view viewWithTag:30];
	UITextView *dutyTV = (UITextView *)[self.view viewWithTag:31];
	UITextView *demandTV = (UITextView *)[self.view viewWithTag:32];
	
	
	NSArray *tfArr = @[jobCategoryTF, workLifeTF, educationTF, salaryTF, districtTF, addressTF, workTypeTF, welfareTV, publisherTF,  publisherJobTF, dutyTV, demandTV];
	
	for(NSString *str in [tfArr valueForKeyPath:@"text"])
	{
		if ([NSString isEmpty:str] || [str isEqualToString:@"点击选择"] ||
			[str isEqualToString:@"填写岗位职责"] || [str isEqualToString:@"填写任职要求"])
		{
			[self presentViewController:[LZCTools alertMessage:@"各项内容均不能为空，请填写完成后再提交"] animated:YES completion:nil];
			return;
		}
	}
	if (self.type == 0) {
		NSDictionary *paramDict = @{
									@"title":jobCategoryTF.text,
									@"category_id":self.selectedCategoryID,
									@"education":educationTF.text,
									@"salary":salaryTF.text,
									@"district":self.district,
									@"address":addressTF.text,
									@"nature":workTypeTF.text,
									@"welfare":welfareTV.text,
									@"publisher":publisherTF.text,
									@"position":publisherJobTF.text,
									@"demand":demandTV.text,
									@"duty":dutyTV.text,
									@"work_life":workLifeTF.text
									};
		
		[[NetworkTool sharedTool] requestWithName:@"发布岗位" URLString:APIAddPosition parameters:paramDict method:@"POST" callBack:^(id responseObject) {
			NSString *resultState = responseObject[@"data"][@"status"];
			if ([resultState integerValue] == 1) {
				UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"发布成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
				[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
					self.isReturn = 0;
					[self dismissViewControllerAnimated:YES completion:nil];
				}]];
				[self presentViewController:alert animated:YES completion:nil];
			}
		}];
	}
	else {
		NSDictionary *paramDict = @{
									@"position_id":self.positionId,
									@"title":jobCategoryTF.text,
									@"category_id":self.selectedCategoryID,
									@"education":educationTF.text,
									@"salary":salaryTF.text,
									@"district":self.district,
									@"address":addressTF.text,
									@"nature":workTypeTF.text,
									@"welfare":welfareTV.text,
									@"publisher":publisherTF.text,
									@"position":publisherJobTF.text,
									@"demand":demandTV.text,
									@"duty":dutyTV.text,
									@"work_life":workLifeTF.text
									};
		
		[[NetworkTool sharedTool] requestWithName:@"编辑岗位信息" URLString:APIUpdatePosition parameters:paramDict method:@"POST" callBack:^(id responseObject) {
			NSString *resultState = responseObject[@"data"][@"status"];
			if ([resultState integerValue] == 1) {
				UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"编辑成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
				[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
					self.isReturn = 0;
					[self dismissViewControllerAnimated:YES completion:nil];
				}]];
				[self presentViewController:alert animated:YES completion:nil];
			}
		}];
	}
}

- (void)setModel:(JobDetailInfo *)model
{
	if (_model != model) {
		_model = model;
	}
	
	UITextField *jobCategoryTF = (UITextField *)[self.view viewWithTag:21];
	UITextField *workLifeTF = (UITextField *)[self.view viewWithTag:22];
	UITextField *educationTF = (UITextField *)[self.view viewWithTag:23];
	UITextField *salaryTF = (UITextField *)[self.view viewWithTag:24];
	UITextField *districtTF = (UITextField *)[self.view viewWithTag:25];
	UITextField *addressTF = (UITextField *)[self.view viewWithTag:26];
	UITextField *workTypeTF = (UITextField *)[self.view viewWithTag:27];
	UITextView *welfareTV = (UITextView *)[self.view viewWithTag:28];
	UITextField *publisherTF = (UITextField *)[self.view viewWithTag:29];
	UITextField *publisherJobTF = (UITextField *)[self.view viewWithTag:30];
	UITextView *dutyTV = (UITextView *)[self.view viewWithTag:31];
	UITextView *demandTV = (UITextView *)[self.view viewWithTag:32];
	
	jobCategoryTF.text = model.category_title;
	workLifeTF.text = model.work_life;
	educationTF.text = model.education;
	salaryTF.text = model.salary;
	districtTF.text = model.district;
	addressTF.text = model.address;
	workTypeTF.text = model.nature;
	welfareTV.text = model.welfare;
	publisherTF.text = model.publisher;
	publisherJobTF.text = model.position;
	dutyTV.text = model.duty;
	demandTV.text = model.demand;
	self.selectedCategoryID = model.category_id;
	self.district = model.district;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)jobCategoryInfoArr
{
	if (!_jobCategoryInfoArr) {
		_jobCategoryInfoArr = [[NSMutableArray alloc] init];
	}
	return _jobCategoryInfoArr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
